package com.johnny.action;

import java.net.Socket;

import com.johnny.data.DataPool;
import com.johnny.data.PlayerInfo;
import com.johnny.data.RoomInfo;
import com.johnny.data.TableInfo;
import com.johnny.startup.Casino28App;

import net.sf.json.JSONObject;

public class BankerSitAction implements Action {

	@Override
	public String doAction(JSONObject object, Socket socket) {
		JSONObject result = new JSONObject();
		result.put("action", object.getString("action"));
		RoomInfo room = null;
		TableInfo table = null;
		for (RoomInfo roomInfo : DataPool.getInstance().getRoomInfos()) {
			if (object.getString("roomId").equals(roomInfo.getRoomId())) {
				room = roomInfo;
				for (TableInfo tableInfo : room.getTables()) {
					if (tableInfo.getTableId().equals(object.getString("tableId"))) {
						table = tableInfo;
					}
				}
				break;
			}
		}
		PlayerInfo player = Casino28App.connector.getPlayerInfoBySocket(socket);
		if (player.getCredits() < (room.getMaxBet() * 2)) {
			result.put("state", 1);
		} else {
			table.getBankerCandidateList().add(player.getName());
			result.put("state", 0);
		}
		return result.toString();
	}

}
